package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.mapper.AccountMapper;
import com.zhentao.mapper.IncomeExpenseItemMapper;
import com.zhentao.mapper.MemberMapper;
import com.zhentao.pojo.Account;
import com.zhentao.pojo.IncomeExpenseItem;
import com.zhentao.pojo.Member;
import com.zhentao.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 财务通用查询接口
 */
@RestController
@RequestMapping("/finance")
public class FinanceController {

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private IncomeExpenseItemMapper incomeExpenseItemMapper;

    @Autowired
    private MemberMapper memberMapper;

    /**
     * 获取所有账户（用于下拉选择）
     */
    @GetMapping("/accounts")
    public Result<List<Account>> getAccounts() {
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        wrapper.eq("is_default", 1); // 只查询默认账户
        wrapper.orderByAsc("name");
        return Result.success(accountMapper.selectList(wrapper));
    }

    /**
     * 获取收入项目（用于下拉选择）
     */
    @GetMapping("/incomeItems")
    public Result<List<IncomeExpenseItem>> getIncomeItems() {
        QueryWrapper<IncomeExpenseItem> wrapper = new QueryWrapper<>();
        wrapper.eq("type", "1"); // 1-收入项目
        wrapper.orderByAsc("name");
        return Result.success(incomeExpenseItemMapper.selectList(wrapper));
    }

    /**
     * 获取支出项目（用于下拉选择）
     */
    @GetMapping("/expenseItems")
    public Result<List<IncomeExpenseItem>> getExpenseItems() {
        QueryWrapper<IncomeExpenseItem> wrapper = new QueryWrapper<>();
        wrapper.eq("type", "2"); // 2-支出项目
        wrapper.orderByAsc("name");
        return Result.success(incomeExpenseItemMapper.selectList(wrapper));
    }

    /**
     * 获取所有会员（用于下拉选择）
     */
    @GetMapping("/members")
    public Result<List<Member>> getMembers() {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1); // 只查询启用的会员
        wrapper.orderByAsc("name");
        return Result.success(memberMapper.selectList(wrapper));
    }

    /**
     * 获取财务统计数据
     */
    @GetMapping("/overview")
    public Result<Map<String, Object>> getOverview() {
        Map<String, Object> overview = new HashMap<>();
        
        // 账户数量
        QueryWrapper<Account> accountWrapper = new QueryWrapper<>();
        Long accountCount = accountMapper.selectCount(accountWrapper);
        overview.put("accountCount", accountCount);
        
        // 收入项目数量
        QueryWrapper<IncomeExpenseItem> incomeWrapper = new QueryWrapper<>();
        incomeWrapper.eq("type", "1");
        Long incomeItemCount = incomeExpenseItemMapper.selectCount(incomeWrapper);
        overview.put("incomeItemCount", incomeItemCount);
        
        // 支出项目数量
        QueryWrapper<IncomeExpenseItem> expenseWrapper = new QueryWrapper<>();
        expenseWrapper.eq("type", "2");
        Long expenseItemCount = incomeExpenseItemMapper.selectCount(expenseWrapper);
        overview.put("expenseItemCount", expenseItemCount);
        
        // 会员数量
        QueryWrapper<Member> memberWrapper = new QueryWrapper<>();
        memberWrapper.eq("status", 1);
        Long memberCount = memberMapper.selectCount(memberWrapper);
        overview.put("memberCount", memberCount);
        
        return Result.success(overview);
    }
}
